package com.bailv.util;

import java.util.Date;

import com.bailv.smartdormitory.R;
import com.bailv.smartdormitory.R.string;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SynthesizerListener;

import android.accounts.Account;
import android.accounts.OnAccountsUpdateListener;
import android.app.Dialog;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class GetUPAlarmThread extends Thread {

	private volatile boolean is_shotdownThread = false;
	private Context context;
	private SoundPool soundPool;
	private int streamID, soundID;
	private boolean isSpeech = false, isSound = false;
	private String[] geyan;
	private int i = 0;
	private Handler handler;

	public final void showdown() {
		soundPool.stop(streamID);
		XFSpeech.getXFSpeech(context).stopSpeaking();
		is_shotdownThread = true;
		interrupt();
	}

	public GetUPAlarmThread(Context context,Handler handler) {
		this.context = context;
		this.handler = handler;
		soundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
		geyan = new String[3];
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Date date = new Date();
		int temputure, humidity;
		temputure = SensorValueContex.getSensorValueContex().getNewestInDoorTemperature();
		Log.d("temputer", temputure+"");
		humidity = SensorValueContex.getSensorValueContex().getNewesInDoortHumidity();
		String TellingTime = String.format(context.getResources().getString(R.string.getup_msg), date, date, temputure,
				humidity);
		Message msg = new Message();
		msg.what = 1;
		Bundle bundle = new Bundle();
		bundle.putString("geyan", TellingTime);
		msg.setData(bundle);
		handler.sendMessage(msg);
		geyan[0] = context.getResources().getString(R.string.getup_geyan1);
		geyan[1] = context.getResources().getString(R.string.getup_geyan2);
		geyan[2] = context.getResources().getString(R.string.getup_geyan3);
		int code = XFSpeech.getXFSpeech(context).Speech(TellingTime, new SynthesizerListener() {

			@Override
			public void onSpeakResumed() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSpeakProgress(int arg0, int arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSpeakPaused() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSpeakBegin() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onCompleted(SpeechError arg0) {
				// TODO Auto-generated method stub
				isSound = true;
			}

			@Override
			public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
				// TODO Auto-generated method stub

			}
		});

		while (!is_shotdownThread) {
			if (isSpeech) {
				msg = new Message();
				msg.what = 1;
				bundle = new Bundle();
				bundle.putString("geyan", geyan[i]);
				msg.setData(bundle);
				handler.sendMessage(msg);
				XFSpeech.getXFSpeech(context).Speech(geyan[i], new SynthesizerListener() {

					@Override
					public void onSpeakResumed() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSpeakProgress(int arg0, int arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSpeakPaused() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSpeakBegin() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onCompleted(SpeechError arg0) {
						// TODO Auto-generated method stub
						isSound = true;
						if (++i >= 3) {
							i = 0;
						}
					}

					@Override
					public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
						// TODO Auto-generated method stub

					}
				});
				isSpeech = false;
			}
			if (isSound) {
				soundID = soundPool.load(context, R.raw.getup, 1);
				soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

					@Override
					public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
						// TODO Auto-generated method stub
						streamID = soundPool.play(soundID, 1, 1, 0, 0, 1);
					}
				});
				try {
					sleep(5000);
					isSpeech = true;
					isSound = false;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}
	

}
